﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using Microsoft.Extensions.Primitives;
using System.Text;

namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 推荐入库
    /// </summary>
    public class RecommendedInStockService : BaseService<BaseEntity>, IRecommendedInStockService
    {
        private readonly IRecommendedInStockRepository repository;
        public RecommendedInStockService(IRecommendedInStockRepository _repository) : base(_repository)
        {
            repository = _repository;
        }
        //根据条码验证条码信息
        public SetObjectDetail GetStockByCarton(string cartonNo, string isGroup)
        {
            List<SysStock> sysStocks = repository.GetStockByCarton(cartonNo);
            SetObjectDetail rfInfo = new SetObjectDetail();
            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不存在，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }
            else if (sysStocks[0].CartonType == 2)
            {
                //提示  [M0090105]:该条码为托条码，请重新扫描！
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码为托条码，请重新扫描！";
                return rfInfo;
            }
            else if (sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.WAITING_INSTOCK)
            {
                //提示  [M0090105]:是**状态，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:是" + sysStocks[0].StockStatus + "状态，请检查!";
                return rfInfo;
            }
            else if (sysStocks[0].Status == (int)WmsEnumUtil.StockStatus.INSTOCKED)
            {
                //提示  [M0090105]:该条码已入库，请勿重复扫描!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码已入库，请勿重复扫描!";
                return rfInfo;
            }
            else if (sysStocks[0].Enabled != "Y")
            {
                //提示  [M0090105]:箱条码已冻结，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:箱条码已冻结，无法操作!";
                return rfInfo;
            }
            else if (sysStocks[0].CartonType == 0)//判断是否有上级条码（周转箱条码）
            {

                List<WmsRackPackage> WmsRackPackageInfos = repository.GetWmsRackPackageType(sysStocks[0].CartonNo).ToList();
                if (WmsRackPackageInfos.Count > 0)
                {
                    //提示  条码有周转箱，请扫描周转箱条码!
                    rfInfo.type = "FAIL";
                    rfInfo.message = "条码有周转箱，请扫描周转箱条码：[" + WmsRackPackageInfos[0].RackNo + "]!";
                    return rfInfo;
                }
            }
            else if (sysStocks[0].CartonType == 1)//根据周转箱条码找出箱条码集合
            {
				sysStocks = new List<SysStock>();
				sysStocks = repository.GetWmsRackPackageByStock(cartonNo);
				if (sysStocks.Count == 0)
				{
					//提示  周转箱条码没有包含的箱条码
					rfInfo.type = "FAIL";
					rfInfo.message = "[" + cartonNo + "]该周转箱条码没有包含的箱条码，请检查!";
					return rfInfo;
				}

			}

			string recommendedLocationName = "";//推荐库位
			List<SysStock> sysstockByGroupNoCount = new List<SysStock>();
			List<SysZonePart> sysZoneParts = new List<SysZonePart>();
			if (isGroup == "1")//要是分组的话,没有对应关系
			{
				if (!string.IsNullOrEmpty(sysStocks[0].GroupNo) && sysStocks[0].GroupNo != "NULL")
				{
					sysstockByGroupNoCount = repository.GetStockPartByGroupNo(sysStocks[0].GroupNo);
				}
			
				var sys = sysstockByGroupNoCount.GroupBy(s => s.PartNo).ToList();//有多个零件号就不推荐库位
				if (sys.Count() == 1)
				{
					sysZoneParts = repository.GetZonePartByPartNo(sysStocks[0].PartNo);//通过零件号找到库区与零件号的对应关系
				}
			}
			else
			{
				sysZoneParts = repository.GetZonePartByPartNo(sysStocks[0].PartNo);//通过零件号找到库区与零件号的对应关系
				
			}

			if (sysZoneParts.Count > 0)
			{
				for (int i = 0; i < sysZoneParts.Count; i++)
				{
					List<SysLocate> sysLocatePart = repository.GetLocateByZoneNameAndPartNo(sysZoneParts[i].ZoneName, sysStocks[0].PartNo);
					if (sysLocatePart.Count > 0)
					{
						for (int k = 0; k < sysLocatePart.Count; k++)
						{
							SysStock sysStocksQtySum = repository.GetStockByLocateName(sysLocatePart[k].LocateName);//库位在库数
							if (sysLocatePart[k].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)//基础库位
							{
								if (sysLocatePart[k].LocateCapacity >= sysStocksQtySum.Qty + sysStocks[0].Qty)
								{
									recommendedLocationName = sysLocatePart[k].LocateName;
									break;
								}
							}
						}
					}
					else if (sysLocatePart.Count == 0)
					{
						List<SysLocate> sysLocates = repository.GetLocateByZoneName(sysZoneParts[i].ZoneName);
						for (int j = 0; j < sysLocates.Count; j++)
						{
							SysStock sysStocksQtySum = repository.GetStockByLocateName(sysLocates[j].LocateName);//库位在库数

							int locateType = sysLocates[j].LocateType;
							if (sysstockByGroupNoCount.Count > 1)//多零件号只推荐动态组合库
							{
								if (sysLocates[j].LocateType == (int)WmsEnumUtil.LocateType.COMBINATION_LOCATE && sysLocates[j].LocateCapacity >= sysStocksQtySum.Qty + sysStocks[0].Qty)
								{
									recommendedLocationName = sysLocates[j].LocateName;
									break;
								}
							}
							else//单个零件号
							{
								if (locateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)
								{
									List<SysStock> sysStocks1 = repository.GetStockByPartNoLocateName(sysLocates[j].LocateName);
									if (sysStocks1.Count > 0)
									{
										for (int k = 0; k < sysStocks1.Count; k++)
										{
											if (sysStocks[0].PartNo == sysStocks1[k].PartNo && sysLocates[j].LocateCapacity >= sysStocksQtySum.Qty + sysStocks[0].Qty)
											{
												recommendedLocationName = sysLocates[j].LocateName;
												break;
											}
										}
										if (!recommendedLocationName.Equals(""))
										{
											break;
										}
									}
									else
									{
										recommendedLocationName = sysLocates[j].LocateName;
										break;
									}
								}
								else if (locateType == (int)WmsEnumUtil.LocateType.COMBINATION_LOCATE)
								{
									if (sysLocates[j].LocateCapacity >= sysStocksQtySum.Qty + sysStocks[0].Qty)
									{
										recommendedLocationName = sysLocates[j].LocateName;
										break;
									}
								}
							}
						}
					}


					if (!recommendedLocationName.Equals(""))
					{
						break;
					}
				}
			}

			List<object> locateNameInfoList = new List<object>();
			SysLocate sysLocate = new SysLocate();
			sysLocate.LocateName = recommendedLocationName;
			locateNameInfoList.Add((object)sysLocate);

			rfInfo.type = "PASS";
			rfInfo.message = "[" + cartonNo + "]:已扫描，请扫描入库库位!";
			rfInfo.wmsObjectDetail = locateNameInfoList;
			return rfInfo;

			#region 原先的逻辑

			/*
		 string recommendedLocationName = "";//推荐库位
		 List<SysStock> sysstockByGroupNoCount = new List<SysStock>();
		 List<SysZonePart> sysZoneParts = new List<SysZonePart>();
		 if (isGroup == "1")//要是分组的话
		 {
			 sysZoneParts = repository.GetZonePartByVendorId(sysStocks[0].VendorId);//通过供应商id找到库区与零件号的对应关系

			 if (!string.IsNullOrEmpty(sysStocks[0].GroupNo) && sysStocks[0].GroupNo != "NULL")
			 {
				 sysstockByGroupNoCount = repository.GetStockPartByGroupNo(sysStocks[0].GroupNo);
			 }
		 }
		 else
		 {
			 sysZoneParts = repository.GetZonePartByPartNo(sysStocks[0].PartNo);//通过零件号找到库区与零件号的对应关系
			 if (sysZoneParts.Count == 0)//要是零件号找不到对应关系，将通过供应商id
			 {
				 sysZoneParts = repository.GetZonePartByVendorId(sysStocks[0].VendorId);
			 }
		 }

		 if (sysZoneParts.Count > 0)
		 {
			 for (int i = 0; i < sysZoneParts.Count; i++)
			 {
				 List<SysLocate> sysLocatePart = repository.GetLocateByZoneNameAndPartNo(sysZoneParts[i].ZoneName, sysStocks[0].PartNo);
				 if (sysLocatePart.Count > 0)
				 {
					 for (int k = 0; k < sysLocatePart.Count; k++)
					 {
						 SysStock sysStocksQtySum = repository.GetStockByLocateName(sysLocatePart[k].LocateName);//库位在库数
						 if (sysLocatePart[k].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)//基础库位
						 {
							 if (sysLocatePart[k].LocateCapacity >= sysStocksQtySum.Qty + sysStocks[0].Qty)
							 {
								 recommendedLocationName = sysLocatePart[k].LocateName;
								 break;
							 }
						 }
					 }
				 }
				 else if (sysLocatePart.Count == 0)
				 {
					 List<SysLocate> sysLocates = repository.GetLocateByZoneName(sysZoneParts[i].ZoneName);
					 for (int j = 0; j < sysLocates.Count; j++)
					 {
						 SysStock sysStocksQtySum = repository.GetStockByLocateName(sysLocates[j].LocateName);//库位在库数

						 int locateType = sysLocates[j].LocateType;
						 if (sysstockByGroupNoCount.Count > 1)//多零件号只推荐动态组合库
						 {
							 if (sysLocates[j].LocateType == (int)WmsEnumUtil.LocateType.COMBINATION_LOCATE && sysLocates[j].LocateCapacity >= sysStocksQtySum.Qty + sysStocks[0].Qty)
							 {
								 recommendedLocationName = sysLocates[j].LocateName;
								 break;
							 }
						 }
						 else//单个零件号
						 {
							 if (locateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)
							 {
								 List<SysStock> sysStocks1 = repository.GetStockByPartNoLocateName(sysLocates[j].LocateName);
								 if (sysStocks1.Count > 0)
								 {
									 for (int k = 0; k < sysStocks1.Count; k++)
									 {
										 if (sysStocks[0].PartNo == sysStocks1[k].PartNo && sysLocates[j].LocateCapacity >= sysStocksQtySum.Qty + sysStocks[0].Qty)
										 {
											 recommendedLocationName = sysLocates[j].LocateName;
											 break;
										 }
									 }
									 if (!recommendedLocationName.Equals(""))
									 {
										 break;
									 }
								 }
								 else
								 {
									 recommendedLocationName = sysLocates[j].LocateName;
									 break;
								 }
							 }
							 else if (locateType == (int)WmsEnumUtil.LocateType.COMBINATION_LOCATE)
							 {
								 if (sysLocates[j].LocateCapacity >= sysStocksQtySum.Qty + sysStocks[0].Qty)
								 {
									 recommendedLocationName = sysLocates[j].LocateName;
									 break;
								 }
							 }
						 }
					 }
				 }


				 if (!recommendedLocationName.Equals(""))
				 {
					 break;
				 }
			 }
		 }

		 List<object> locateNameInfoList = new List<object>();
		 SysLocate sysLocate = new SysLocate();
		 sysLocate.LocateName = recommendedLocationName;
		 locateNameInfoList.Add((object)sysLocate);

		 rfInfo.type = "PASS";
		 rfInfo.message = "[" + cartonNo + "]:已扫描，请扫描入库库位!";
		 rfInfo.wmsObjectDetail = locateNameInfoList;
		 return rfInfo;
		 */

			#endregion




		}
		//输入库位，完成入库
		public SetObjectDetail CheckStockByCartonAlocate(string cartonNo, string locateName, string isGroup, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            List<SysStock> sysStocks = repository.GetStockByCarton(cartonNo);
            if (sysStocks.Count == 0)
            {
                //提示  [M0090105]:该条码不存在，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }
            else if (sysStocks[0].Status != (int)WmsEnumUtil.StockStatus.WAITING_INSTOCK)
            {
                //提示  [M0090105]:是**状态，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:是" + sysStocks[0].StockStatus + "状态，请检查!";
                return rfInfo;
            }
            else if (sysStocks[0].Status == (int)WmsEnumUtil.StockStatus.INSTOCKED)
            {
                //提示  [M0090105]:该条码已入库，请勿重复扫描!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码已入库，请勿重复扫描!";
                return rfInfo;
            }
            else if (sysStocks[0].Enabled != "Y")
            {
                //提示  [M0090105]:箱条码已冻结，无法操作!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:箱条码已冻结，无法操作!";
                return rfInfo;
            }

            List<SysLocate> sysLocates = repository.GetLocateInfoByLocateName(locateName);

            SysStock sysStocksQtySum = repository.GetStockByLocateName(locateName);//库位在库数

            if (sysLocates.Count == 0)
            {
                //提示  [A1-2-2]:该库位不存在，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不存在，请检查!";
                return rfInfo;
            }
            else if (sysLocates[0].Enabled != "Y")
            {
                //提示  [A1-2-2]:该库位不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不可用，请检查!";
                return rfInfo;
            }
            else if ((decimal)sysStocksQtySum.Qty > sysLocates[0].LocateCapacity)
            {
                //提示  [A1-2-2]:该库位容量已满，请检查！
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位容量已满，请检查！";
                return rfInfo;
            }
            else if (sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.BASICS_LOCATE && sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE && sysLocates[0].LocateType != (int)WmsEnumUtil.LocateType.COMBINATION_LOCATE)
            {
                //提示  [A1-2-2]:该库位不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位为" + sysLocates[0].LocateTypeDesc + "，请检查!";
                return rfInfo;
            }


            //判断组托容量
            if (isGroup == "1")
            {
                if (!string.IsNullOrEmpty(sysStocks[0].GroupNo) && sysStocks[0].GroupNo != "NULL")
                {
                    List<SysStock> sysStocks1 = repository.GetStockByLocate(sysStocks[0].LocateName);

                    if ((decimal)(sysStocksQtySum.Qty + sysStocks1[0].Qty) > sysLocates[0].LocateCapacity)
                    {
                        //提示  [A1-2-2]:该库位容量不足，请检查！
                        rfInfo.type = "FAIL";
                        rfInfo.message = "[" + locateName + "]:该库位容量不足，请检查！";
                        return rfInfo;
                    }

                    List<SysStock> sysStocks2 = repository.GetStockInfoByGroupNo(sysStocks[0].GroupNo);
                    for (int k = 0; k < sysStocks2.Count; k++)
                    {
                        string locatePartNo1 = sysLocates[0].PartNo;
                        string stockPartNo1 = sysStocks2[k].PartNo;

                        if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)
                        {
                            if (locatePartNo1 != stockPartNo1)
                            {
                                if (sysStocks2.Count - 1 == k)
                                {
                                    //提示  此箱码所对应零件与库位零件不匹配!
                                    rfInfo.type = "FAIL";
                                    rfInfo.message = "此箱码所对应零件与库位零件不匹配!";
                                    return rfInfo;
                                }
                            }
                        }
                        else if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)//机动库位
                        {
                            List<SysStock> sysStocks3 = repository.GetStockInfoByLocateName(sysLocates[0].LocateName);
                            for (int i = 0; i < sysStocks3.Count; i++)
                            {
                                if (sysStocks3[i].PartNo != "" && stockPartNo1 != sysStocks3[i].PartNo)
                                {
                                    if (sysStocks2.Count - 1 == k)
                                    {
                                        //提示  此箱码所对应零件与库位零件不匹配!
                                        rfInfo.type = "FAIL";
                                        rfInfo.message = "[" + stockPartNo1 + "]该零件号无法入到机动库位，请检查!";
                                        return rfInfo;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    //提示  [A1-2-2]:该条码未分组，请检查！
                    rfInfo.type = "FAIL";
                    rfInfo.message = "[" + cartonNo + "]:该条码未分组，请检查！";
                    return rfInfo;
                }
            }

            string locatePartNo = sysLocates[0].PartNo;
            string stockPartNo = sysStocks[0].PartNo;
            if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)
            {
                if (locatePartNo != stockPartNo)
                {
                    //提示  此箱码所对应零件与库位零件不匹配!
                    rfInfo.type = "FAIL";
                    rfInfo.message = "此箱码所对应零件与库位零件不匹配!";
                    return rfInfo;
                }
            }
            if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)//机动库位
            {
                List<SysStock> sysStocks1 = repository.GetStockInfoByLocateName(sysLocates[0].LocateName);
                for (int i = 0; i < sysStocks1.Count; i++)
                {
                    if (sysStocks1[i].PartNo != "" && stockPartNo != sysStocks1[i].PartNo)
                    {
                        //提示  此箱码所对应零件与库位零件不匹配!
                        rfInfo.type = "FAIL";
                        rfInfo.message = "[" + stockPartNo + "]该零件号无法入到机动库位，请检查!";
                        return rfInfo;
                    }
                }
            }

			if (sysStocks[0].CartonType == 1)
			{
				sysStocks = new List<SysStock>();
				sysStocks = repository.GetWmsRackPackageByStock(cartonNo);
				if (sysStocks.Count == 0)
				{
					//提示  周转箱条码没有包含的箱条码
					rfInfo.type = "FAIL";
					rfInfo.message = "[" + cartonNo + "]该周转箱条码没有包含的箱条码，请检查!";
					return rfInfo;
				}
			}
			decimal sumQty = sysStocks.Sum(s => s.Qty);
			if ((sysLocates[0].LocateCapacity - (decimal)sysStocksQtySum.Qty) < sumQty)//库位容量-当前库位数量<当前库位容量
			{
				//提示 库位容量小于条码数量，请检查！
				rfInfo.type = "FAIL";
				rfInfo.message = "[" + locateName + "]库位容量小于条码数量，请检查！";
				return rfInfo;
			}





			List<string> sqlStrings = new List<string>();
			List<DynamicParameters> parameterList = new List<DynamicParameters>();
			StringBuilder stringBuilder = new StringBuilder();
			List<SysStock> sysstockByGroupNo = new List<SysStock>();

			List<SysStock> sysStockInfos = new List<SysStock>();//所有箱条码的集合，用于接口数据





			/*
            if (isGroup == "1")
            {
                sysstockByGroupNo = repository.GetStockInfoByGroupNo(sysStocks[0].GroupNo);

                for (int i = 0; i < sysstockByGroupNo.Count; i++)
                {

                    stringBuilder = new StringBuilder();
                    stringBuilder.Append("UPDATE sys_stock SET status=50,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "'" +
                          ",zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "'" +
                        ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysstockByGroupNo[i].CartonNo + "' and status=40;");
                    sqlStrings.Add(stringBuilder.ToString());

                    stringBuilder = new StringBuilder();
                    stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
                    stringBuilder.Append("( ");
                    stringBuilder.Append("    trans_code, ");
                    stringBuilder.Append("    carton_no, ");
                    stringBuilder.Append("    part_id, ");
                    stringBuilder.Append("    part_no, ");
                    stringBuilder.Append("    part_spec, ");
                    stringBuilder.Append("    src_locate_id, ");
                    stringBuilder.Append("    src_locate_name, ");
                    stringBuilder.Append("    dest_locate_id, ");
                    stringBuilder.Append("    dest_locate_name, ");
                    stringBuilder.Append("    old_qty, ");
                    stringBuilder.Append("    new_qty, ");
                    stringBuilder.Append("    trans_qty, ");
                    stringBuilder.Append("    old_status, ");
                    stringBuilder.Append("    new_status, ");
                    stringBuilder.Append("    unit, ");
                    stringBuilder.Append("    factory_id, ");
                    stringBuilder.Append("    factory_code, ");
                    stringBuilder.Append("    src_erp_warehouse, ");
                    stringBuilder.Append("    dest_erp_warehouse, ");
                    stringBuilder.Append("    src_warehouse_id, ");
                    stringBuilder.Append("    src_warehouse_name, ");
                    stringBuilder.Append("    dest_warehouse_id, ");
                    stringBuilder.Append("    dest_warehouse_name, ");
                    stringBuilder.Append("    src_zone_id, ");
                    stringBuilder.Append("    src_zone_name, ");
                    stringBuilder.Append("    dest_zone_id, ");
                    stringBuilder.Append("    dest_zone_name, ");
                    stringBuilder.Append("    enabled, ");
                    stringBuilder.Append("    create_userid, ");
                    stringBuilder.Append("    create_time, ");
                    stringBuilder.Append("    update_userid, ");
                    stringBuilder.Append("    update_time, ");
                    stringBuilder.Append("    guid ");
                    stringBuilder.Append(") ");
                    stringBuilder.Append("VALUES ");
                    stringBuilder.Append("(   '60', ");
                    stringBuilder.Append("    '" + sysstockByGroupNo[i].CartonNo + "', ");
                    stringBuilder.Append("    " + sysstockByGroupNo[i].PartId + ", ");
                    stringBuilder.Append("    '" + sysstockByGroupNo[i].PartNo + "',  ");
                    stringBuilder.Append("    N'" + sysstockByGroupNo[i].PartSpec + "', ");
                    stringBuilder.Append("    " + sysstockByGroupNo[i].LocateId == null ? 0 : sysstockByGroupNo[i].LocateId + ",  ");
                    stringBuilder.Append("    '" + sysstockByGroupNo[i].LocateName + "',  ");
                    stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");
                    stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
                    stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
                    stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
                    stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
                    stringBuilder.Append("    '40', ");
                    stringBuilder.Append("    '50', ");
                    stringBuilder.Append("    '" + sysstockByGroupNo[i].Unit + "', ");
                    stringBuilder.Append("    " + sysstockByGroupNo[i].FactoryId + ", ");
                    stringBuilder.Append("    '" + sysstockByGroupNo[i].FactoryCode + "',");
                    stringBuilder.Append("    '" + sysstockByGroupNo[i].ErpWarehouse + "',");
                    stringBuilder.Append("    '" + sysstockByGroupNo[i].ErpWarehouse + "', ");
                    stringBuilder.Append("    " + sysstockByGroupNo[i].WarehouseId == null ? 0 : sysstockByGroupNo[i].WarehouseId + ", ");
                    stringBuilder.Append("    '" + sysstockByGroupNo[i].WarehouseName + "', ");
                    stringBuilder.Append("    " + sysLocates[0].WarehouseId + ", ");
                    stringBuilder.Append("    '" + sysLocates[0].WarehouseName + "', ");
                    stringBuilder.Append("    " + sysstockByGroupNo[i].ZoneId == null ? 0 : sysstockByGroupNo[i].ZoneId + ",  ");
                    stringBuilder.Append("    '" + sysstockByGroupNo[i].ZoneName + "',  ");
                    stringBuilder.Append("    " + sysLocates[0].ZoneId + ", ");
                    stringBuilder.Append("    '" + sysLocates[0].ZoneName + "',  ");
                    stringBuilder.Append("    'Y',  ");
                    stringBuilder.Append("    " + loginId + ", ");
                    stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                    stringBuilder.Append("    " + loginId + ", ");
                    stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                    stringBuilder.Append("    NEWID()  ");
                    stringBuilder.Append("    ) ; ");
                    sqlStrings.Add(stringBuilder.ToString());

                }
            }
            else
            {



                stringBuilder = new StringBuilder();
                stringBuilder.Append("UPDATE sys_stock SET status=50,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "'" +
                    ",zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "'" +
                                  ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + cartonNo + "'");
                sqlStrings.Add(stringBuilder.ToString());

                stringBuilder = new StringBuilder();
                stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
                stringBuilder.Append("( ");
                stringBuilder.Append("    trans_code, ");
                stringBuilder.Append("    carton_no, ");
                stringBuilder.Append("    part_id, ");
                stringBuilder.Append("    part_no, ");
                stringBuilder.Append("    part_spec, ");
                stringBuilder.Append("    src_locate_id, ");
                stringBuilder.Append("    src_locate_name, ");
                stringBuilder.Append("    dest_locate_id, ");
                stringBuilder.Append("    dest_locate_name, ");
                stringBuilder.Append("    old_qty, ");
                stringBuilder.Append("    new_qty, ");
                stringBuilder.Append("    trans_qty, ");
                stringBuilder.Append("    old_status, ");
                stringBuilder.Append("    new_status, ");
                stringBuilder.Append("    unit, ");
                stringBuilder.Append("    factory_id, ");
                stringBuilder.Append("    factory_code, ");
                stringBuilder.Append("    src_erp_warehouse, ");
                stringBuilder.Append("    dest_erp_warehouse, ");
                stringBuilder.Append("    src_warehouse_id, ");
                stringBuilder.Append("    src_warehouse_name, ");
                stringBuilder.Append("    dest_warehouse_id, ");
                stringBuilder.Append("    dest_warehouse_name, ");
                stringBuilder.Append("    src_zone_id, ");
                stringBuilder.Append("    src_zone_name, ");
                stringBuilder.Append("    dest_zone_id, ");
                stringBuilder.Append("    dest_zone_name, ");
                stringBuilder.Append("    enabled, ");
                stringBuilder.Append("    create_userid, ");
                stringBuilder.Append("    create_time, ");
                stringBuilder.Append("    update_userid, ");
                stringBuilder.Append("    update_time, ");
                stringBuilder.Append("    guid ");
                stringBuilder.Append(") ");
                stringBuilder.Append("VALUES ");
                stringBuilder.Append("(   '60', ");
                stringBuilder.Append("    '" + sysStocks[0].CartonNo + "', ");
                stringBuilder.Append("    " + sysStocks[0].PartId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].PartNo + "',  ");
                stringBuilder.Append("    N'" + sysStocks[0].PartSpec + "', ");
                stringBuilder.Append("    " + sysStocks[0].LocateId == null ? 0 : sysStocks[0].LocateId + ",  ");
                stringBuilder.Append("    '" + sysStocks[0].LocateName + "',  ");
                stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");
                stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
                stringBuilder.Append("    " + sysStocks[0].Qty + ",");
                stringBuilder.Append("    " + sysStocks[0].Qty + ",");
                stringBuilder.Append("    " + sysStocks[0].Qty + ",");
                stringBuilder.Append("    '40', ");
                stringBuilder.Append("    '50', ");
                stringBuilder.Append("    '" + sysStocks[0].Unit + "', ");
                stringBuilder.Append("    " + sysStocks[0].FactoryId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].FactoryCode + "',");
                stringBuilder.Append("    '" + sysStocks[0].ErpWarehouse + "',");
                stringBuilder.Append("    '" + sysStocks[0].ErpWarehouse + "', ");
                stringBuilder.Append("    " + sysStocks[0].WarehouseId == null ? 0 : sysStocks[0].WarehouseId + ", ");
                stringBuilder.Append("    '" + sysStocks[0].WarehouseName + "', ");
                stringBuilder.Append("    " + sysLocates[0].WarehouseId + ", ");
                stringBuilder.Append("    '" + sysLocates[0].WarehouseName + "', ");
                stringBuilder.Append("    " + sysStocks[0].ZoneId == null ? 0 : sysStocks[0].ZoneId + ",  ");
                stringBuilder.Append("    '" + sysStocks[0].ZoneName + "',  ");
                stringBuilder.Append("    " + sysLocates[0].ZoneId + ", ");
                stringBuilder.Append("    '" + sysLocates[0].ZoneName + "',  ");
                stringBuilder.Append("    'Y', ");
                stringBuilder.Append("    " + loginId + ", ");
                stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                stringBuilder.Append("    " + loginId + ", ");
                stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                stringBuilder.Append("    NEWID()  ");
                stringBuilder.Append("    ) ; ");
                sqlStrings.Add(stringBuilder.ToString());
            }

            */





			if (isGroup == "1" && sysStocks[0].CartonType != 2)//分组
			{
				sysstockByGroupNo = repository.GetUpdateStockByGroupNo(sysStocks[0].GroupNo);
				if (sysstockByGroupNo.Count > 0)
				{
					for (int i = 0; i < sysstockByGroupNo.Count; i++)
					{

						stringBuilder = new StringBuilder();
						stringBuilder.Append("UPDATE sys_stock SET status=50,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "'" +
							  ",zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "'" +
							",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysstockByGroupNo[i].CartonNo + "';");
						sqlStrings.Add(stringBuilder.ToString());

						stringBuilder = new StringBuilder();
						stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
						stringBuilder.Append("( ");
						stringBuilder.Append("    trans_code, ");
						stringBuilder.Append("    carton_no, ");
						stringBuilder.Append("    part_id, ");
						stringBuilder.Append("    part_no, ");
						stringBuilder.Append("    part_spec, ");
						stringBuilder.Append("    src_locate_id, ");
						stringBuilder.Append("    src_locate_name, ");
						stringBuilder.Append("    dest_locate_id, ");
						stringBuilder.Append("    dest_locate_name, ");
						stringBuilder.Append("    old_qty, ");
						stringBuilder.Append("    new_qty, ");
						stringBuilder.Append("    trans_qty, ");
						stringBuilder.Append("    old_status, ");
						stringBuilder.Append("    new_status, ");
						stringBuilder.Append("    unit, ");
						stringBuilder.Append("    factory_id, ");
						stringBuilder.Append("    factory_code, ");
						stringBuilder.Append("    src_erp_warehouse, ");
						stringBuilder.Append("    dest_erp_warehouse, ");
						stringBuilder.Append("    src_warehouse_id, ");
						stringBuilder.Append("    src_warehouse_name, ");
						stringBuilder.Append("    dest_warehouse_id, ");
						stringBuilder.Append("    dest_warehouse_name, ");
						stringBuilder.Append("    src_zone_id, ");
						stringBuilder.Append("    src_zone_name, ");
						stringBuilder.Append("    dest_zone_id, ");
						stringBuilder.Append("    dest_zone_name, ");
						stringBuilder.Append("    enabled, ");
						stringBuilder.Append("    create_userid, ");
						stringBuilder.Append("    create_time, ");
						stringBuilder.Append("    update_userid, ");
						stringBuilder.Append("    update_time, ");
						stringBuilder.Append("    guid ");
						stringBuilder.Append(") ");
						stringBuilder.Append("VALUES ");
						stringBuilder.Append("(   '60', ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].CartonNo + "', ");
						stringBuilder.Append("    " + sysstockByGroupNo[i].PartId + ", ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].PartNo + "',  ");
						stringBuilder.Append("    N'" + sysstockByGroupNo[i].PartSpec + "', ");
						stringBuilder.Append("    '" + (sysstockByGroupNo[i].LocateId == null ? 0 : sysstockByGroupNo[i].LocateId) + "',  ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].LocateName + "',  ");
						stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
						stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
						stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
						stringBuilder.Append("    " + sysstockByGroupNo[i].Qty + ",");
						stringBuilder.Append("    '40', ");
						stringBuilder.Append("    '50', ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].Unit + "', ");
						stringBuilder.Append("    " + sysstockByGroupNo[i].FactoryId + ", ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].FactoryCode + "',");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].ErpWarehouse + "',");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].ErpWarehouse + "', ");
						stringBuilder.Append("    " + sysstockByGroupNo[i].WarehouseId == null ? 0 : sysstockByGroupNo[i].WarehouseId + ", ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].WarehouseName + "', ");
						stringBuilder.Append("    " + sysstockByGroupNo[0].WarehouseId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].WarehouseName + "', ");
						stringBuilder.Append("    " + sysstockByGroupNo[i].ZoneId == null ? 0 : sysstockByGroupNo[i].ZoneId + ",  ");
						stringBuilder.Append("    '" + sysstockByGroupNo[i].ZoneName + "',  ");
						stringBuilder.Append("    " + sysLocates[0].ZoneId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].ZoneName + "',  ");
						stringBuilder.Append("    'Y', ");
						stringBuilder.Append("    " + loginId + ", ");
						stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
						stringBuilder.Append("    " + loginId + ", ");
						stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
						stringBuilder.Append("    NEWID()  ");
						stringBuilder.Append("    ) ; ");
						sqlStrings.Add(stringBuilder.ToString());

						if (sysstockByGroupNo[i].CartonType == 0)
						{
							sysStockInfos.Add(sysstockByGroupNo[i]);//所有箱条码的集合，用于接口数据
						}
					}

				}

			}
			else
			{
				if (sysStocks.Count > 0)
				{
					List<SysStock> sysStockInfo = new List<SysStock>();
					sysStockInfo = repository.GetWmsByStock(sysStocks[0].CartonNo);//找到周转箱
					if (sysStockInfo.Count>0)
					{
						sysStocks.Add(sysStockInfo[0]);
					}

					for (int i = 0; i < sysStocks.Count; i++)
					{
						if (sysStocks[0].CartonType == 0)
						{
							sysStockInfos.Add(sysStocks[i]);//所有箱条码的集合，用于接口数据
						}

						stringBuilder = new StringBuilder();
						stringBuilder.Append("UPDATE sys_stock SET status=50,locate_id=" + sysLocates[0].LocateId + ",locate_name='" + locateName + "'" +
							  ",zone_id=" + sysLocates[0].ZoneId + ",zone_name='" + sysLocates[0].ZoneName + "',warehouse_id=" + sysLocates[0].WarehouseId + ",warehouse_name='" + sysLocates[0].WarehouseName + "'" +
							",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysStocks[i].CartonNo + "';");
						sqlStrings.Add(stringBuilder.ToString());

						stringBuilder = new StringBuilder();
						stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
						stringBuilder.Append("( ");
						stringBuilder.Append("    trans_code, ");
						stringBuilder.Append("    carton_no, ");
						stringBuilder.Append("    part_id, ");
						stringBuilder.Append("    part_no, ");
						stringBuilder.Append("    part_spec, ");
						stringBuilder.Append("    src_locate_id, ");
						stringBuilder.Append("    src_locate_name, ");
						stringBuilder.Append("    dest_locate_id, ");
						stringBuilder.Append("    dest_locate_name, ");
						stringBuilder.Append("    old_qty, ");
						stringBuilder.Append("    new_qty, ");
						stringBuilder.Append("    trans_qty, ");
						stringBuilder.Append("    old_status, ");
						stringBuilder.Append("    new_status, ");
						stringBuilder.Append("    unit, ");
						stringBuilder.Append("    factory_id, ");
						stringBuilder.Append("    factory_code, ");
						stringBuilder.Append("    src_erp_warehouse, ");
						stringBuilder.Append("    dest_erp_warehouse, ");
						stringBuilder.Append("    src_warehouse_id, ");
						stringBuilder.Append("    src_warehouse_name, ");
						stringBuilder.Append("    dest_warehouse_id, ");
						stringBuilder.Append("    dest_warehouse_name, ");
						stringBuilder.Append("    src_zone_id, ");
						stringBuilder.Append("    src_zone_name, ");
						stringBuilder.Append("    dest_zone_id, ");
						stringBuilder.Append("    dest_zone_name, ");
						stringBuilder.Append("    enabled, ");
						stringBuilder.Append("    create_userid, ");
						stringBuilder.Append("    create_time, ");
						stringBuilder.Append("    update_userid, ");
						stringBuilder.Append("    update_time, ");
						stringBuilder.Append("    guid ");
						stringBuilder.Append(") ");
						stringBuilder.Append("VALUES ");
						stringBuilder.Append("(   '60', ");
						stringBuilder.Append("    '" + sysStocks[i].CartonNo + "', ");
						stringBuilder.Append("    " + sysStocks[i].PartId + ", ");
						stringBuilder.Append("    '" + sysStocks[i].PartNo + "',  ");
						stringBuilder.Append("    N'" + sysStocks[i].PartSpec + "', ");
						stringBuilder.Append("    '" + (sysStocks[i].LocateId == null ? 0 : sysStocks[i].LocateId) + "',  ");
						stringBuilder.Append("    '" + sysStocks[i].LocateName + "',  ");
						stringBuilder.Append("    " + sysLocates[0].LocateId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].LocateName + "',  ");
						stringBuilder.Append("    " + sysStocks[i].Qty + ",");
						stringBuilder.Append("    " + sysStocks[i].Qty + ",");
						stringBuilder.Append("    " + sysStocks[i].Qty + ",");
						stringBuilder.Append("    '40', ");
						stringBuilder.Append("    '50', ");
						stringBuilder.Append("    '" + sysStocks[i].Unit + "', ");
						stringBuilder.Append("    " + sysStocks[i].FactoryId + ", ");
						stringBuilder.Append("    '" + sysStocks[i].FactoryCode + "',");
						stringBuilder.Append("    '" + sysStocks[i].ErpWarehouse + "',");
						stringBuilder.Append("    '" + sysStocks[i].ErpWarehouse + "', ");
						stringBuilder.Append("    " + sysStocks[i].WarehouseId == null ? 0 : sysStocks[i].WarehouseId + ", ");
						stringBuilder.Append("    '" + sysStocks[i].WarehouseName + "', ");
						stringBuilder.Append("    " + sysLocates[0].WarehouseId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].WarehouseName + "', ");
						stringBuilder.Append("    " + sysStocks[i].ZoneId == null ? 0 : sysStocks[i].ZoneId + ",  ");
						stringBuilder.Append("    '" + sysStocks[i].ZoneName + "',  ");
						stringBuilder.Append("    " + sysLocates[0].ZoneId + ", ");
						stringBuilder.Append("    '" + sysLocates[0].ZoneName + "',  ");
						stringBuilder.Append("    'Y', ");
						stringBuilder.Append("    " + loginId + ", ");
						stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
						stringBuilder.Append("    " + loginId + ", ");
						stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
						stringBuilder.Append("    NEWID()  ");
						stringBuilder.Append("    ) ; ");
						sqlStrings.Add(stringBuilder.ToString());


					}


				}


			}


			var _sysStockInfos = sysStockInfos.GroupBy(x => x.PartId).Select(s => (new { PartId = s.Key, qtySum = s.Sum(item => item.Qty), RecQtySum = s.Sum(item => Convert.ToDecimal(item.RecQty)) })).ToList();

			for (int i = 0; i < _sysStockInfos.Count(); i++)
			{

				SysStock sys = sysStockInfos.Where(s => s.PartId == _sysStockInfos[i].PartId).First();
				List<WmsOutstockDetail> outstockDetail = repository.GetOutstockDetailList(sys.RecCartonQty, _sysStockInfos[i].PartId.ToString());

				stringBuilder = new StringBuilder();
				stringBuilder.Append("INSERT INTO dbo.WMS_DSPOS ");
				stringBuilder.Append("( EBELN, ");//通知单号
				stringBuilder.Append("    POVBELN, ");//采购订单号
				stringBuilder.Append("    EVRTP, ");//行号
				stringBuilder.Append("    LIFNR, ");//供应商代码
				stringBuilder.Append("    MATNR, ");//物料编码
				stringBuilder.Append("    ZDEV_NUM, ");//订单数量
				stringBuilder.Append("    UNIT, ");//计量单位
				stringBuilder.Append("    PSTYP, ");//特殊采购标识  1：委外，2：采购
				stringBuilder.Append("    ZDATE, ");//计划交货日期
				stringBuilder.Append("    ZCJSJ, ");//计划交货时间
				stringBuilder.Append("    WERKS, ");//工厂代码
				stringBuilder.Append("    LGORT, ");//收货库
				stringBuilder.Append("    ZSTAS, ");//状态  1、已收货，2、未收货
				stringBuilder.Append("    MLOGO, ");//收货数量
													//stringBuilder.Append("    DATAST, ");
													//stringBuilder.Append("    SHNUM, ");
				stringBuilder.Append("    SID, ");
				stringBuilder.Append("    ACTTIM, ");
				stringBuilder.Append("    ACTUSR, ");
				stringBuilder.Append("    STAFLG, ");
				stringBuilder.Append("    guid ");
				stringBuilder.Append(") ");
				stringBuilder.Append("VALUES ");
				stringBuilder.Append(" (   '" + sys.CartonNo + "', ");
				stringBuilder.Append("    '" + sys.RefOrderNo + "', ");
				stringBuilder.Append("    '" + (outstockDetail.Count==0?"":  outstockDetail[0].ItemNo )+ "',  ");
				stringBuilder.Append("    '" + sys.VendorCode + "', ");
				stringBuilder.Append("    '" + sys.PartNo + "',");
				stringBuilder.Append("    '" + _sysStockInfos[i].qtySum + "',  ");
				stringBuilder.Append("    '" + sys.Unit + "', ");
				stringBuilder.Append("    '" + 2 + "',  ");
				stringBuilder.Append("    '" + (outstockDetail.Count==0?"" : outstockDetail[0].PlanDate) + "',");
				stringBuilder.Append("    '" + (outstockDetail.Count==0?"": outstockDetail[0].PlanTime) + "',");
				stringBuilder.Append("    '" + sys.FactoryCode + "',");
				stringBuilder.Append("    '" + sys.ZoneName + "', ");//收货库 (库区)
				stringBuilder.Append("    '1', ");//1、已收货，2、未收货
				stringBuilder.Append("    '" + _sysStockInfos[i].RecQtySum + "', ");
				stringBuilder.Append("'" + Guid.NewGuid().ToString().Replace("-", "") + "',");
				stringBuilder.Append("'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',");
				stringBuilder.Append("'" + loginId + "',");
				stringBuilder.Append("'" + 0 + "',");
				stringBuilder.Append("    NEWID()  ");
				stringBuilder.Append("    ) ; ");
				sqlStrings.Add(stringBuilder.ToString());
			}




			int refNum = repository.SQLTransaction(sqlStrings, parameterList);

            if (refNum > 0)
            {

                if (isGroup == "1")
                {
                    sysstockByGroupNo = repository.GetStockInfoByInGroupNo(sysStocks[0].GroupNo);
                }
                else
                {
                    sysstockByGroupNo = repository.GetStockInfoByInCartonNo(sysStocks[0].CartonNo);
                }
                rfInfo.type = "PASS";
                rfInfo.message = "操作成功!请继续扫描箱条码";
                rfInfo.wmsObjectDetail = sysstockByGroupNo.ConvertAll(s => (object)s);

                return rfInfo;
            }
            else
            {
                //提示  操作失败!
                rfInfo.type = "FAIL";
                rfInfo.message = "操作失败!请检查库存条码";
                return rfInfo;
            }
        }
    }
}
